package com.jbh.libapi

import com.google.gson.Gson
import okhttp3.Callback
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import retrofit2.GsonConverterFactory
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import java.util.concurrent.TimeUnit

object HttpRequest {


    private const val OUT_TIME = 20L


    /**
     * okhttp配置
     */
    private val okBuilder = OkHttpClient.Builder().apply {
        connectTimeout(OUT_TIME, TimeUnit.MINUTES)
        readTimeout(OUT_TIME, TimeUnit.MINUTES)
        retryOnConnectionFailure(false)
    }

    /**
     * 配置Retrofit
     */
    private val retrofitBuilder: Retrofit.Builder = Retrofit.Builder()
        .client(okBuilder.build())
        .baseUrl("http://api.zhaojinapp.com:8020/")
        .addConverterFactory(GsonConverterFactory.create(Gson()))
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .addConverterFactory(NullOnEmptyConverterFactory())


    /**
     * post 请求
     */
    fun post(api: String, json: String, callback: Callback) {
        //1、获取ok实体对象
        val client = okBuilder.build()
        //2、创建请求对象
        val request: Request = Request.Builder()
            .url(api)
            .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json))
            .build()
        //3、创建请求实例
        val call = client.newCall(request)
        //4、同步请求
//        call.request()
        call.enqueue(callback)
    }


    /**
     * post 请求
     */
    fun get(api: String, callback: Callback) {
        //1、获取ok实体对象
        val client = okBuilder.build()
        //2、创建请求对象
        val request: Request = Request.Builder()
            .url(api)
            .get()
            .build()
        //3、创建请求实例
        val call = client.newCall(request)
        //4、同步请求
//        call.request()
        call.enqueue(callback)
    }


    /**
     * 网络请求
     */
    fun request(): ApiServer {
        val okClient: OkHttpClient = OkHttpClient.Builder()
            .connectTimeout(OUT_TIME, TimeUnit.MINUTES)
            .readTimeout(OUT_TIME, TimeUnit.MINUTES)
            .retryOnConnectionFailure(false)
            .build()

        val builder: Retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(Gson()))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(NullOnEmptyConverterFactory())
            .client(okClient)
            .baseUrl("http://api.zhaojinapp.com:8020/")
            .build()
        return builder.create(ApiServer::class.java)
    }


    /**
     * 网络请求
     */
    fun <T> request(cls: Class<T>): T {
        val okClient: OkHttpClient = OkHttpClient.Builder()
            .connectTimeout(OUT_TIME, TimeUnit.MINUTES)
            .readTimeout(OUT_TIME, TimeUnit.MINUTES)
            .retryOnConnectionFailure(false)
            .build()

        val builder: Retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(Gson()))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(NullOnEmptyConverterFactory())
            .client(okClient)
            .baseUrl(ApiConfig.baseUrl)
            .build()
        return builder.create(cls)
    }
}